package cz.muni.fi.pa165.travelagency.entities;

import cz.muni.fi.pa165.travelagency.api.entities.ICustomer;
import cz.muni.fi.pa165.travelagency.api.entities.IOrder;
import cz.muni.fi.pa165.travelagency.api.entities.IPackageTour;
import cz.muni.fi.pa165.travelagency.api.entities.ITour;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.persistence.CascadeType;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.ManyToOne;
import javax.persistence.Table;
import javax.persistence.Temporal;
import javax.persistence.TemporalType;

/**
 * Class for storing orders
 *
 * @author Marián Mozola
 * @version 1.0
 */
@Entity
@Table(name = "Orders")
public class Order implements IOrder {

    private static final long serialVersionUID = 2L;
    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;
    @ManyToOne(targetEntity = Customer.class)
    private ICustomer customer;
    @ManyToOne(targetEntity = PackageTour.class)
    private IPackageTour orderedPackageTour;
    
    @ManyToMany(targetEntity = Tour.class)
    @JoinTable(name = "ORDERS_TOURS",
    joinColumns = {
        @JoinColumn(name = "TOUR_ID")
    },
    inverseJoinColumns = {
        @JoinColumn(name = "ORDERS_ID")
    })
    private List<ITour> orderedTours = new ArrayList<>();
    @Temporal(TemporalType.TIMESTAMP)
    private Date created;
    private int numberOfPersons;
    private boolean paid;
    private BigDecimal price;

    public Order() {
    }

    @Override
    public Long getId() {
        return id;
    }

    @Override
    public void setId(Long id) {
        this.id = id;
    }

    @Override
    public ICustomer getCustomer() {
        return customer;
    }

    @Override
    public void setCustomer(ICustomer customer) {
        this.customer = customer;
    }

    @Override
    public IPackageTour getOrderedPackageTour() {
        return orderedPackageTour;
    }

    @Override
    public void setOrderedPackageTour(IPackageTour orderedPackageTour) {
        this.orderedPackageTour = orderedPackageTour;
    }

    @Override
    public List<ITour> getOrderedTours() {
        return orderedTours;
    }

    @Override
    public void setOrderedTours(List<ITour> orderedTours) {
        this.orderedTours = orderedTours;
    }

    @Override
    public Date getCreated() {
        return created;
    }

    @Override
    public void setCreated(Date created) {
        this.created = created;
    }

    @Override
    public int getNumberOfPersons() {
        return numberOfPersons;
    }

    @Override
    public void setNumberOfPersons(int numberOfPersons) {
        this.numberOfPersons = numberOfPersons;
    }

    @Override
    public boolean isPaid() {
        return paid;
    }

    @Override
    public void setPaid(boolean paid) {
        this.paid = paid;
    }

    @Override
    public BigDecimal getPrice() {
        return price;
    }

    @Override
    public void setPrice(BigDecimal price) {
        this.price = price;
    }

    @Override
    public String toString() {
        return "Order{" + "id=" + id + ", customer=" + customer + ", paid=" + paid + ", price=" + price + '}';
    }

    @Override
    public int hashCode() {
        int hash = 5;
        hash = 53 * hash + (this.id != null ? this.id.hashCode() : 0);
        return hash;
    }

    @Override
    public boolean equals(Object obj) {
        if (obj == null) {
            return false;
        }
        if (getClass() != obj.getClass()) {
            return false;
        }
        final Order other = (Order) obj;
        if (this.id != other.id && (this.id == null || !this.id.equals(other.id))) {
            return false;
        }
        return true;
    }
}
